package com.zb.frame.modules.monitor.upgrade;

import com.zb.frame.base.upgrade.UpgradeColumn;
import com.zb.frame.base.upgrade.UpgradeTable;
import com.zb.frame.base.upgrade.common.UpgradeTask;
import com.zb.frame.base.upgrade.enums.DBType;
import com.zb.frame.base.upgrade.enums.ValueType;
import com.zb.frame.modules.monitor.utils.UpgradeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;


public class UpgradeTask_New extends UpgradeTask {
	
	private static final Logger logger = LoggerFactory.getLogger(UpgradeTask_New.class);

	@Override
	public boolean doUpgrade(Connection conn, DBType type) {
		try {
			createTableMonitorLog(conn, type, "monitor_log", "系统日志表");
			return true;
		} catch(Exception e) {
			logger.error("Upgrade module '" + this.getClass().getPackage().getName() + "' to " + getNewVersion() + " fail.", e);
			return false;
		}
	}

	/**
	 *创建表-系统日志表
	 * @param conn		jdbc连接
	 * @param type		数据库类型
	 * @param tableName	sys_menu
	 * @throws SQLException
	 */
	private void createTableMonitorLog(Connection conn, DBType type, String tableName, String comment) throws SQLException {
		if(UpgradeUtils.checkTableExist(conn,tableName, this.getClass().getPackage().getName()))
			return;
		UpgradeTable table = new UpgradeTable(tableName, comment);
		table.addColumns(new UpgradeColumn[] {
				new UpgradeColumn("log_id", "主键", ValueType.LONG, false, null),
				new UpgradeColumn("log_module", "操作模块", ValueType.STRING_VARCHAR, true, 40),
				new UpgradeColumn("log_type", "日志类型0-登录1-新增-2修改-3删除4-授权5-取消授权6-停用7-启用8-用户上锁9-用户解锁", ValueType.INT, true,null),
				new UpgradeColumn("old_value", "旧数据JSON", ValueType.TEXT, true, null),
				new UpgradeColumn("new_value", "新数据JSON", ValueType.TEXT, true, null),
				new UpgradeColumn("rel_id", "关联的主键", ValueType.STRING_VARCHAR, true, 40),
				new UpgradeColumn("batch_flag", "批量标识", ValueType.INT, true, null),
				new UpgradeColumn("return_flag", "恢复标识", ValueType.INT, true, null),
				new UpgradeColumn("ok_flag", "成功标识", ValueType.INT, true, null),
				new UpgradeColumn("fail_msg", "失败原因", ValueType.STRING_VARCHAR, true, 100),
				new UpgradeColumn("log_desc", "描述", ValueType.STRING_VARCHAR, true, 100),
				new UpgradeColumn("log_ext1", "扩展字段1", ValueType.STRING_VARCHAR, true, 20),
				new UpgradeColumn("operator", "操作人", ValueType.LONG, true, null),
				new UpgradeColumn("create_time", "创建时间", ValueType.DATETIME, true, null)
		});
		table.addPrimaryKeys("log_id");
		table.addIdentityKeys("log_id");
		table.doCreateTable(conn, type);
	}

	@Override
	public String getNewVersion() {
		return "0.0.1";
	}

}
